import 'dart:collection';

import 'package:dio/dio.dart';
import 'package:tender_flutter/constant/api_path.dart';
import 'package:tender_flutter/constant/app_config.dart';
import 'package:tender_flutter/http/api_response.dart';
import 'package:tender_flutter/http/http_utils.dart';

class ApiSupplier {
  /// 供应商行业类别
  static Future getSupplierIndustryCategory<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.suppplierIndustry);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 获取供应商列表
  /// industryId 企业供应商行业ID
  /// professionId  个人供应商专业ID
  /// type  供应商类型（个人获或企业）
  static Future getSupplierList<T>(
      {int areaCode,
      int industryId,
      int professionId,
      int type,
      String keyword,
      int page = AppConfig.page,
      int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["area_id"] = areaCode;
      params["label_id"] = industryId == 0 ? null : industryId;
      params["type"] = type == 0 ? null : type;
      params["title"] = keyword;
      params["page"] = page;
      params["size"] = pageCount;
      final response =
          await HttpUtils.get(ApiPath.suppplierList, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 获取供应商详情
  static Future getSupplierDetail<T>(int supplierId) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["id"] = supplierId;
      final response =
          await HttpUtils.get(ApiPath.suppplierDetail, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 获取供应商联系方式
  static Future getSupplierContact<T>(int supplierId) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["company_id"] = supplierId;
      final response =
          await HttpUtils.get(ApiPath.suppplierContact, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 加入供应商
  /// areaCode 区域code
  /// address  区域名称
  /// companyLogo  公司Logo
  /// companyName   公司名称
  /// contact  联系人
  /// faRen 公司法人
  /// licensePic 营业执照
  /// name 联系人
  /// phone 联系电话
  /// remark 主营项目
  /// title 企业名称
  /// wxAccount 微信
  /// industryIds 行业类别id 格式：[{"id":0}]
  /// profiles 资质证书信息列表 格式：[{"code":"string","endDate":"2020-11-10T06:11:32.570Z","id":0,"issueAuthority":"string","issueDate":"2020-11-10T06:11:32.570Z","licensePic":"string","qualificationType":0,"regLevel":0,"regType":0,"remark":"string","title":"string"}]
  /// type  供应商类型（2：个人 或 1 ：企业）
  static Future joinSupplier<T>({
    int areaCode,
    String address,
    String companyLogo,
    String companyName,
    String contact,
    String faRen,
    String licensePic,
    String name,
    String phone,
    String remark,
    String title,
    int type,
    String wxAccount,
    List<Map<String, dynamic>> industryIds,
    List<Map<String, dynamic>> profiles,
  }) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["address"] = address;
      params["area_id"] = areaCode;
      params["companyLogo"] = companyLogo;
      params["companyName"] = companyName;
      params["contact"] = contact;
      params["faRen"] = faRen;
      params["labels"] = industryIds;
      params["licensePic"] = licensePic;
      params["name"] = name;
      params["phone"] = phone;
      params["profiles"] = profiles;
      params["remark"] = remark;
      params["title"] = title;
      params["type"] = type;
      params["wxAccount"] = wxAccount;
      final response =
          await HttpUtils.postRaw(ApiPath.joinSuppplier, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }
}
